package com.cqxyd.smartsite.controller;

import com.alibaba.fastjson.JSONObject;
import com.cqxyd.smartsite.pojo.ProgramInfo;
import com.cqxyd.smartsite.service.ProgramInfoService;
import com.cqxyd.smartsite.util.KeyValueUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@ApiIgnore
@RequestMapping(value = "/programInfo")
public class ProgramInfoController {
    @Autowired
    private ProgramInfoService programInfoService;

    // [start] 查询
    @RequestMapping(value = "/list")
    public String list(Model model) {
        return "programInfo/programInfo-list";
    }

    @RequestMapping(value = "/programInfoList")
    @ResponseBody
    public String programInfoList(Integer pageNum, Integer pageSize, String programName)
            throws Exception {
        PageInfo<ProgramInfo> pageInfo = programInfoService.getProgramInfoListByPage(pageNum, pageSize, programName);
        JSONObject result = new JSONObject();
        result.put("rows", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        return result.toJSONString();
    }
    // [end]

    // [start] 新增

    @RequestMapping(value = "/add")
    public String insert(Model model) {
        model.addAttribute("deviceTypes", KeyValueUtil.getDeviceTypes());
        return "programInfo/programInfo-add";
    }

    @ResponseBody
    @RequestMapping(value = "/insert")
    public String insert(HttpServletRequest request, HttpServletResponse response, ProgramInfo programInfo) {
        try {
            programInfo.setCreateTime(new Date());
            programInfoService.insert(programInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redireck:programInfo/programInfo-list";
    }

    // [end]

    // [start] 更新

    @RequestMapping(value = "/edit")
    public String edit(Model model, @RequestParam("programId") Integer programId) {
        try {
            ProgramInfo programInfo = programInfoService.selectOne(programId);
            model.addAttribute("currentProgramInfo", programInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "programInfo/programInfo-edit";
    }

    @RequestMapping(value = "/updateSave")
    @ResponseBody
    public String updateSave(ProgramInfo currentProgramInfo) {
        try {
            int result = programInfoService.update(currentProgramInfo);
            if (result != 1) {
                return "{\"success\":false}";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "{\"success\":true}";
    }

    // [end]

    // [start] 删除

    @RequestMapping(value = "/delete")
    @ResponseBody
    public String delete(@RequestParam("programId") Integer programId) {
        try {
            int result = programInfoService.delete(programId);
            if (result != 1) {
                return "{\"success\":false}";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"success\":false}";
        }
        return "{\"success\":true}";
    }
}
